home *** CD-ROM | disk | FTP | other *** search
- /*
- * Collection abstract class
- *
- * Andrew Nicholson - Not-So-Soft, Oz.
- *
- * Provides the following methods to any subclass that responds to
- * head, tail, push, isEmpty
- *
- * append c item - appends item to collection c
- * copy c - returns a new collection which is a copy of c
- * add c itemlist - adds items to collection
- * sequence c - returns an initialised sequence object containing c
- * repList c - returns a representation list for c (ascii).
- * print c - outputs nicely formatted collection c to stdout
- * deepDispose c - dispose c and sends 'deepDispose' to all it contains
- * dispose c - free the collection leaving what it contains
- * deepCopy c - returns a deep copy of c
- */
-
- #include "oic.h"
- #include "generics.h"
-
- class Collect;
-
- /* -------------------- Collection methods ---------------------------- */
-
- static object
- _append(self, inst, item) /* append item to the collection */
- object self;
- void *inst;
- object *item;
- {
- register object c;
-
- for (c = self ; ! (int)isEmpty(c) ; c = tail(c));
- push(c, *item);
-
- return self;
- }
-
- static object
- _copy(self, inst, args) /* Copy the collection */
- object self;
- void *inst, *args;
- {
- if ((int)isEmpty(self))
- return New(ClassOf(self));
- else
- return push(copy(tail(self)), head(self));
- }
-
- static object
- _add(self, inst, items) /* add items to collection */
- object self;
- void *inst;
- register object *items;
- {
- while (*items != END)
- append(self, *items++);
- return self;
- }
-
- static object
- _sequence(self) /* get a sequence over collection */
- object self;
- {
- return start(New(Linkseq), self);
- }
-
- static object
- _repList(self) /* get a represention list */
- object self;
- {
- register object c;
- register object replist;
-
- replist = New(Replist, "(", ")", " ");
-
- if ((int)isEmpty(self))
- append(replist, New(String, "EMPTY"));
- else for (c = self; ! (int)isEmpty(c); c = tail(c))
- append(replist, repList(head(c)));
-
- return replist;
- }
-
- static void
- _deepDispose(self)
- object self;
- {
- register object c, nextc;
- register object item;
-
- for (c = self; !(int)isEmpty(c); c = nextc)
- {
- nextc = tail(c);
- deepDispose(head(c));
- free(c);
- }
- free(c);
- }
-
- static void
- _dispose(self)
- object self;
- {
- register object c, nextc;
-
- for (c = self; ! (int)isEmpty(c) ; c = nextc)
- {
- nextc = tail(c);
- free(c);
- }
- free(c);
- }
-
- /* ------------------- Init the Collection class ---------------------- */
-
- _InitCollect()
- {
- Collect = NewClass(0, 0, "Collect", END);
- AddMethods(Collect,
- appendGeneric, _append,
- copyGeneric, _copy,
- addGeneric, _add,
- sequenceGeneric, _sequence,
- repListGeneric, _repList,
- deepDisposeGeneric, _deepDispose,
- disposeGeneric, _dispose,
- END
- );
- }
-
-